package com.example.demo.web;

import com.example.demo.bean.Res;
import com.example.demo.bean.entity.TTest;
import com.example.demo.repository.OauthService;
import com.example.demo.repository.i.TTestService;
import com.example.demo.test.ProcessContext;
import com.example.demo.test.alc.login.AccountLoginContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class CardTestController {
    @Autowired
    TTestService tTestService;
    @Autowired
    AccountLoginContext accountLoginContext;
    @Autowired
    ProcessContext processContext;
    @Autowired
    OauthService oauthService;

    @GetMapping("card/list")
    public ModelAndView get() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("cardlist", tTestService.list());
        modelAndView.setViewName("/card/cardlist");
        return modelAndView;
    }

    @GetMapping("card/detail/{id}")
    public ModelAndView getdetail(@PathVariable("id") String id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("card", tTestService.getById(id));
        modelAndView.addObject("cardType", processContext.getTestType());
        modelAndView.setViewName("/card/carddetail");
        return modelAndView;
    }

    @GetMapping("card/edit/{id}")
    public ModelAndView getById(@PathVariable("id") String id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("card", tTestService.getById(id));
        modelAndView.addObject("cardType", processContext.getTestType());
        modelAndView.setViewName("/card/cardedit");
        return modelAndView;
    }

    @GetMapping("card/test/{id}")
    public ModelAndView test(@PathVariable("id") String id) {
        ModelAndView modelAndView = new ModelAndView();
        TTest tTest = tTestService.getById(id);
        modelAndView.addObject("card", tTest);
        modelAndView.addObject("flow", processContext.getTestProcessById(tTest.getFid()));
        modelAndView.setViewName("/card/cardtest");
        return modelAndView;
    }

    @GetMapping("api/card/test/run/{id}")
    public void test2(@PathVariable("id") String id) {
        processContext.run(tTestService.getById(id));
    }


    @GetMapping("card/{cid}/flow/select")
    public ModelAndView select(@RequestParam String type, @PathVariable("cid") String cid) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("card", tTestService.getById(cid));
        modelAndView.addObject("flowlist", processContext.getTestProcessByType(type));
        modelAndView.setViewName("/card/flowlistsel");
        return modelAndView;
    }

    @GetMapping("card/flow/{fid}")
    public ModelAndView flow(@PathVariable("fid") Integer fid) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("flow", processContext.getTestProcessById(fid));
        modelAndView.setViewName("/component/flow");
        return modelAndView;
    }


    @GetMapping("card/testuser/{id}")
    public ModelAndView testuser(@PathVariable("id") String id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("card", tTestService.getById(id));
        modelAndView.setViewName("/card/testuser");
        return modelAndView;
    }

    @GetMapping("card/testinfo/{id}")
    public ModelAndView testinfo(@PathVariable("id") String id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("card", tTestService.getById(id));
        modelAndView.setViewName("/card/testinfo");
        return modelAndView;
    }

    @PostMapping("card")
    public Res<Boolean> put(@RequestBody TTest testInfo) {
        boolean success;
        if (testInfo.getId() == null) {
            success = tTestService.save(testInfo);
        } else {
            success = tTestService.updateById(testInfo);
        }
        return Res.success(success);
    }

    @DeleteMapping("card/{id}")
    public Res<Boolean> delete(@PathVariable String id) {
        return Res.success(tTestService.removeById(id));
    }


}
